package com.niuyubo;

import com.common.collection.Student;
import com.common.collection.Classes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StudentDemo {

    public static void main(String[] args) {
        filterByAge();
        distinctName();
        nameTimes();
        sortByAge();
        addressAndInfo();
    }

    static void filterByAge() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        for (Student student : stlist) {
            if (student.getAge() < 15) {
                System.out.println(student);
            }
        }
    }

    static List<String> distinctName() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        List<String> names = new ArrayList<>();
        stlist.forEach(student -> {
            if(!names.contains(student.getName())){
                names.add(student.getName());
            }
        });
        System.out.println(names);
        return names;
    }

    static void nameTimes() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        List<String> names = StudentDemo.distinctName();
        names.forEach(str ->{
            int count = 0;
            String name = str;
            for (Student student1 : stlist) {
                String aname = student1.getName();
                if (name.equals(aname)) {count++;}
            }
            System.out.println(name + ":" + count);
        });

    }

    static void sortByAge() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        List<Integer> age = new ArrayList<>();
        for (Student student : stlist) {
            age.add(student.getAge());
        }
        Collections.sort(age);
        age.forEach(a -> {
            for(Student student : stlist) {
                if(a.equals(student.getAge())) {
                    System.out.println(student);
                }
            }
        });

    }

    static void addressAndInfo() {
        Classes classes = new Classes();
        List<Student> stlist = classes.students();
        List<String> address = new ArrayList<>();
        stlist.forEach(student -> {
            if(!address.contains(student.getAddress())){
                address.add(student.getAddress());
            }
        });
        address.forEach(addr ->{
            stlist.forEach( student -> {
                if(addr.equals(student.getAddress())) {
                    System.out.println(addr + "(" + student +")");
                }
            });
        });

    }

}
